package nsh.cl.nd.khtn.mazedash.baseclasses;

import nsh.cl.nd.khtn.mazedash.GameConstant;
import nsh.cl.nd.khtn.mazedash.RootGameActivity;
import nsh.cl.nd.khtn.mazedash.baseclasses.backgroundloader.AsyncTaskLoader;
import nsh.cl.nd.khtn.mazedash.baseclasses.backgroundloader.IAsyncCallback;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.FadeInModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.FontManager;
import org.andengine.opengl.font.StrokeFont;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier;

import android.content.res.AssetManager;

public abstract class BaseFadeLoadingScene extends Scene implements IEntityModifierListener {

	abstract protected void loadResourcesBackGround();

	abstract protected String getBackroundImagePath();

	abstract protected BaseGameActivity getActivityInstance();

	abstract protected void switchToMainScene();

	final int centerX = GameConstant.CAMERA_WIDTH / 2;
	final int centerY = GameConstant.CAMERA_HEIGHT / 2;

	protected BitmapTextureAtlas loadingTextureAtlas;
	protected TextureRegion backgroundTextureRegion;
	protected Sprite background;
	private Text text;

	@Override
	public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
	}

	@Override
	public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
		if (pModifier.getClass() == FadeInModifier.class) {
			loadSelfResources();
		}
	}

	public BaseFadeLoadingScene loadResources() {
		RootGameActivity gameActivity = RootGameActivity.getInstance();
		setBackground(new Background(Color.BLACK));
		FontFactory.setAssetBasePath("font/");
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		loadingTextureAtlas = new BitmapTextureAtlas(getActivityInstance().getTextureManager(),
				GameConstant.CAMERA_WIDTH, GameConstant.CAMERA_HEIGHT);
		backgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(loadingTextureAtlas,
				getActivityInstance(), getBackroundImagePath(), 0, 0);
		loadingTextureAtlas.load();

		background = new Sprite(centerX - backgroundTextureRegion.getWidth() / 2, centerY
				- backgroundTextureRegion.getHeight() / 2, backgroundTextureRegion, getActivityInstance()
				.getVertexBufferObjectManager());

		ITexture smallfontTexture = new BitmapTextureAtlas(gameActivity.getTextureManager(),
				GameConstant.TEXTUREATLAS_MAX_WIDTH, GameConstant.TEXTUREATLAS_MAX_HEIGHT, TextureOptions.BILINEAR);
		FontManager fontManager = gameActivity.getFontManager();
		AssetManager assets = gameActivity.getAssets();
		final StrokeFont smallFont = FontFactory.createStrokeFromAsset(fontManager, smallfontTexture, assets,
				GameConstant.FONT_FILE, GameConstant.FONT_SIZE_BIG, true, Color.WHITE_ARGB_PACKED_INT, 2,
				Color.BLACK_ARGB_PACKED_INT);
		smallFont.load();

		text = new Text(0, 0, smallFont, ".AHO.", ".AHO.".length(), new TextOptions(HorizontalAlign.CENTER),
				gameActivity.getVertexBufferObjectManager());
		text.setPosition((GameConstant.CAMERA_WIDTH - text.getWidth()) / 2,
				(GameConstant.CAMERA_HEIGHT - text.getHeight()) / 2 - 50);
		attachChild(background);
		attachChild(text);

		getActivityInstance().getEngine().setScene(this);

		background.setAlpha(0);
		background.clearEntityModifiers();
		background.registerEntityModifier(new FadeInModifier(0.5f, this));
		return this;
	}

	protected void loadSelfResources() {
		getActivityInstance().runOnUiThread(new Runnable() {
			@Override
			public void run() {
				IAsyncCallback callback = new IAsyncCallback() {

					@Override
					public void workToDo() {
						loadResourcesBackGround();
					}

					@Override
					public void onComplete() {
						try {
							Thread.sleep(GameConstant.SLEEP_AFTER_LOADING);
						} catch (InterruptedException e) {
							// Do nothing
						}
						switchToMainScene();
					}
				};

				new AsyncTaskLoader().execute(callback);
			}
		});
	}
}
